/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ws.converter;

import entidade.persistencia.TipoUsuario;
import java.net.URI;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import entidade.persistencia.Permissao;
import entidade.persistencia.Usuario;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author victorinox
 */

@XmlRootElement(name = "tipoUsuario")
public class TipoUsuarioConverter {
    private TipoUsuario entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of TipoUsuarioConverter */
    public TipoUsuarioConverter() {
        entity = new TipoUsuario();
    }

    /**
     * Creates a new instance of TipoUsuarioConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public TipoUsuarioConverter(TipoUsuario entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getTipoUsuarioId() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getPermissaoCollection();
        getUsuarioCollection();
    }

    /**
     * Creates a new instance of TipoUsuarioConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public TipoUsuarioConverter(TipoUsuario entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for tipoUsuarioId.
     *
     * @return value for tipoUsuarioId
     */
    @XmlElement
    public Integer getTipoUsuarioId() {
        return (expandLevel > 0) ? entity.getTipoUsuarioId() : null;
    }

    /**
     * Setter for tipoUsuarioId.
     *
     * @param value the value to set
     */
    public void setTipoUsuarioId(Integer value) {
        entity.setTipoUsuarioId(value);
    }

    /**
     * Getter for nome.
     *
     * @return value for nome
     */
    @XmlElement
    public String getNome() {
        return (expandLevel > 0) ? entity.getNome() : null;
    }

    /**
     * Setter for nome.
     *
     * @param value the value to set
     */
    public void setNome(String value) {
        entity.setNome(value);
    }

    /**
     * Getter for isFuncionario.
     *
     * @return value for isFuncionario
     */
   /* @XmlElement
    public Boolean getIsFuncionario() {
        return (expandLevel > 0) ? entity.getIsFuncionario() : null;
    }*/

    /**
     * Setter for isFuncionario.
     *
     * @param value the value to set
     */
    /*public void setIsFuncionario(Boolean value) {
        entity.setIsFuncionario(value);
    }*/

    /**
     * Getter for permissaoCollection.
     *
     * @return value for permissaoCollection
     */
    @XmlElement
    public PermissaoesConverter getPermissaoCollection() {
        if (expandLevel > 0) {
            if (entity.getPermissaoCollection() != null) {
                return new PermissaoesConverter(entity.getPermissaoCollection(), uri.resolve("permissaoCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for permissaoCollection.
     *
     * @param value the value to set
     */
    public void setPermissaoCollection(PermissaoesConverter value) {
        entity.setPermissaoCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for usuarioCollection.
     *
     * @return value for usuarioCollection
     */
    @XmlElement
    public UsuarioesConverter getUsuarioCollection() {
        if (expandLevel > 0) {
            if (entity.getUsuarioCollection() != null) {
                return new UsuarioesConverter(entity.getUsuarioCollection(), uri.resolve("usuarioCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for usuarioCollection.
     *
     * @param value the value to set
     */
    public void setUsuarioCollection(UsuarioesConverter value) {
        entity.setUsuarioCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the TipoUsuario entity.
     *
     * @return an entity
     */
    @XmlTransient
    public TipoUsuario getEntity() {
        if (entity.getTipoUsuarioId() == null) {
            TipoUsuarioConverter converter = UriResolver.getInstance().resolve(TipoUsuarioConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved TipoUsuario entity.
     *
     * @return an resolved entity
     */
    public TipoUsuario resolveEntity(EntityManager em) {
        List<Permissao> permissaoCollection = entity.getPermissaoCollection();
        List<Permissao> newpermissaoCollection = new java.util.ArrayList<Permissao>();
        for (Permissao item : permissaoCollection) {
            newpermissaoCollection.add(em.getReference(Permissao.class, item.getPermissaoId()));
        }
        entity.setPermissaoCollection(newpermissaoCollection);
        List<Usuario> usuarioCollection = entity.getUsuarioCollection();
        List<Usuario> newusuarioCollection = new java.util.ArrayList<Usuario>();
        for (Usuario item : usuarioCollection) {
            newusuarioCollection.add(em.getReference(Usuario.class, item.getUsuarioId()));
        }
        entity.setUsuarioCollection(newusuarioCollection);
        return entity;
    }
}
